/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pablow.dao.serializables;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrador
 */
public abstract class GenericDAOSer<T, PK extends Serializable> {

    public abstract T save(T t);
    public abstract List<T> getAll();

    public T persist(T bo){
        List<T> allObjects=null;

        File file=new File(getFileName(bo.getClass()));
        if (file.exists()){
            allObjects=getAll(bo.getClass());
        }else{
            allObjects=new ArrayList<T>();
        }

        if (!allObjects.contains(bo)){
            allObjects.add(bo);
        }

        Logger.getLogger(this.getClass().getCanonicalName()).
                log(Level.INFO,String.valueOf(allObjects.size()));

        FileOutputStream fos=null;
        ObjectOutputStream out=null;

        try {
            fos=new FileOutputStream(file);
            out=new ObjectOutputStream(fos);
            out.writeObject(allObjects);
            out.close();
        } catch (IOException e) {
            Logger.getLogger(this.getClass().getCanonicalName()).log(Level.SEVERE,null,e);
        }
        return bo;
    }

    public Boolean remove(T t){
        Boolean res=false;

        List<T> list=getAll(t.getClass());
        
        ListIterator<T> iterator=list.listIterator();

        recorrerIterador:
        while (iterator.hasNext()){
            T obj=iterator.next();
            if (t.equals(obj)){
                iterator.remove();
                res=true;
                break recorrerIterador;
            }
        }
        FileOutputStream fos = null;
        ObjectOutputStream out = null;

        File file=new File(getFileName(t.getClass()));
            if(!file.delete()){
                System.out.println("No se ha eliminado el archivo  " + file.getName());
            }

        try {
            fos = new FileOutputStream(file);
            out = new ObjectOutputStream(fos);
            out.writeObject(list);
            fos.close();
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(this.getClass().getCanonicalName()).log(Level.SEVERE,null,ex);
        }

        return res;
    }


    public List<T> getByClassName(Class clase) throws IOException{
        FileInputStream fin=null;
        ObjectInputStream oin=null;
        List<T> list=null;
        try {
            fin = new FileInputStream(getFileName(clase));
            oin=new ObjectInputStream(fin);
            list = (ArrayList<T>) oin.readObject();
        }catch (ClassNotFoundException ex) {
            Logger.getLogger(GenericDAOSer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GenericDAOSer.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if (fin!=null) {fin.close();}
            if (oin!=null) {oin.close();}
        }

        return list;
    }

    public Properties getConfiguration(){
        Properties p=new Properties();
        try {
            p.load(getClass().getClassLoader().getResourceAsStream("configuration.properties"));
        } catch (IOException ex) {
            Logger.getLogger(GenericDAOSer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return p;
    }


    public List<T> getAll(Class clase){
        FileInputStream fis=null;
        ObjectInputStream in=null;
        List<T> listResult=null;
        try {
            fis = new FileInputStream(getFileName(clase));
            in = new ObjectInputStream(fis);
            listResult=(ArrayList<T>)in.readObject();
        } catch (ClassNotFoundException ex){
            Logger.getLogger(GenericDAOSer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GenericDAOSer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(GenericDAOSer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listResult;
    }

    private String getFileName(Class clase){
        StringBuilder builder=new StringBuilder()
                            .append(getConfiguration().getProperty("dao.savefiles"))
                            .append(clase.getSimpleName())
                            .append("s.ser");
       return builder.toString();
    }



}
